import {useRef, useEffect, useCallback} from "react"

export default function useDebouncedFn(fn, delay = 200, dep = []) {
  const {current} = useRef({fn, timer: null});
  useEffect(function () {
    current.fn = fn;
  }, [fn]);

  return useCallback(function f(...args) {
    if (current.timer) {
      clearTimeout(current.timer);
    }
    current.timer = setTimeout(() => {
      current.fn.call(this, ...args);
    }, delay);
  }, dep)
}
